package com.lysj.bhp.tpos.controller;

import com.lysj.bhp.approve.domain.ApproveRecord;
import com.lysj.bhp.log.note.SystemLog;
import com.lysj.bhp.log.util.IpUtil;
import com.lysj.bhp.tpos.base.TposContent;
import com.lysj.bhp.tpos.domain.YgmExchagePlatformMoneyAccount;
import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.user.util.MyWallet;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.wallet.domain.BhpcWallet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

/**
 * @Author: ZhangWenJian
 * @Create: 2018/9/17--14:57
 * @Description:第三方用户相关控制类
 */
@Log4j
@RestController
@RequestMapping("api/tpos/v1/public")
@Api(value = "TposUserController", tags = "YGM相关接口")
public class TposUserController extends TposContent{
    @Resource
    MyWallet myWallet;
    @ApiOperation(value = "手机号组成", notes = "1.需要手机号区号 2.手机号 3.用户密码 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "区号", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "password", value = "用户密码", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "timeStamp", value = "时间搓", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "nonce", value = "随机数", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "signature", value = "签名规则", required = true, paramType = "form", dataType = "string"),
    })
    @SystemLog(description = "用户注册")
    @PostMapping("/register_by_phone")
    public Resp registerByPhone(String areaCode ,String phone,String password){
        if (ParamUtil.isBlack(areaCode)) {
            return new Resp(Resp.PARAM_ERROR, "请选择区号");
        }
        if (ParamUtil.isBlack(phone)) {
            return new Resp(Resp.PARAM_ERROR, "请输入手机号");
        }
        User user = userRepository.findByPhoneAndAreaCode(phone,areaCode);
        if(user!=null){
            return new Resp(Resp.PARAM_ERROR, "注册失败!该手机号已被注册");
        }
        if (ParamUtil.isBlack(password)) {
            return new Resp(Resp.PARAM_ERROR, "请输入密码");
        }
        user = new User();
        user.setAreaCode(areaCode);
        user.setPhone(phone);
        user.setPassword(password);
        user.setRegisterDate(new Date());
        user.setLoginIp(IpUtil.getIpAdrress(request));
        user.setStatus(1);
        user.setActiveCode(ParamUtil.uuid());// 激活码
        user.setNextSendEmailMs(System.currentTimeMillis()); // 设置下次发送邮件的时间
        user.setDividend(new BigDecimal(BigInteger.ZERO)); //设置用户利息(eth为单位)

        user.setBtcDividend(new BigDecimal(BigInteger.ZERO)); //设置用户利息(btc为单位)
        user.setBhpc(new BigDecimal(BigInteger.ZERO));
        user.setEthereumBalance(new BigDecimal(BigInteger.ZERO));
        user.setBitcoinBalance(new BigDecimal(BigInteger.ZERO));
        user.setBhpcAward(new BigDecimal(BigInteger.ZERO));
        userRepository.save(user);
        ApproveRecord approveRecord = new ApproveRecord();
        approveRecord.setUserId(user.getUserId());
        approveRecord.setInfoRenewalDate(new Date());
        approveRecord.setInfoStatus(1);
        approveRecordRepository.save(approveRecord);
        return new Resp("注册成功!");
    }



    @ApiOperation(value = "YGM平台资产操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "区号", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "operationType", value = "操作类型 1.加 2.减", required = true, paramType = "form", dataType = "Integer"),
            @ApiImplicitParam(name = "number", value = "兑换数量", required = true, paramType = "form", dataType = "BigDecimal"),
            @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "timeStamp", value = "时间搓", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "nonce", value = "随机数", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "signature", value = "签名规则", required = true, paramType = "form", dataType = "string"),
    })



    @SystemLog(description = "YGM平台资产操作")
    @PostMapping("/exchage_platform_money")
    public Resp exchagePlatformMoney(String areaCode , String phone,  Integer operationType , BigDecimal number) throws Exception {
        if (ParamUtil.isBlack(areaCode)) {
            log.error("兑换失败,区号为空!");
            return new Resp(Resp.PARAM_ERROR,"兑换失败,区号为空!");
        }
        if (ParamUtil.isBlack(phone)) {
            log.error("兑换失败,手机号为空!");
            return new Resp(Resp.PARAM_ERROR,"兑换失败,手机号为空!");
        }
        if (ParamUtil.isBlack(operationType)) {
            log.error("兑换失败,操作类型为空!");
            return new Resp(Resp.PARAM_ERROR,"兑换失败,操作类型为空!");
        }
        if (number.compareTo(BigDecimal.ZERO) <= 0) {
            log.error("兑换失败,兑换数量为空!");
            return new Resp(Resp.PARAM_ERROR,"兑换失败,兑换数量为空!");
        }
        User user = userRepository.findByPhoneAndAreaCode(phone,areaCode);
        if (user == null){
            log.error("兑换失败,用户数据异常!");
            return new Resp(Resp.PARAM_ERROR,"兑换失败,用户数据异常!");
        }
        BhpcWallet bhpcWallet = walletContent.bhpcWalletRepository.findByUserId(user.getUserId());
        if(bhpcWallet == null){
            bhpcWallet = myWallet.createBhpcWallet(user.getUserId());
            log.error("没有钱包数据,创建钱包");
        }
        YgmExchagePlatformMoneyAccount ygmExchagePlatformMoneyAccount = new YgmExchagePlatformMoneyAccount();
        ygmExchagePlatformMoneyAccount.setNumber(number);
        if(operationType == 2){
            if(bhpcWallet.getAvailBalance().compareTo(number) == -1 || bhpcWallet.getBalance().compareTo(number) == -1){
                log.error("兑换失败,资产不足!");
                return new Resp(Resp.PARAM_ERROR,"兑换失败,资金不足!");
            }
            number = number.multiply(new BigDecimal("-1"));

        }
        bhpcWallet.setBalance(bhpcWallet.getBalance().add(number));
        bhpcWallet.setAvailBalance(bhpcWallet.getAvailBalance().add(number));

        ygmExchagePlatformMoneyAccount.setOperationType(operationType);
        ygmExchagePlatformMoneyAccount.setUserId(user.getUserId());
        ygmExchagePlatformMoneyAccount.setCreateTime(new Date());
        ygmExchagePlatformMoneyAccount.setWalletType(100);
        ygmExchagePlatformMoneyAccountRepository.save(ygmExchagePlatformMoneyAccount);
        walletContent.bhpcWalletRepository.save(bhpcWallet);
        return new Resp("兑换成功!");
    }

    @ApiOperation(value = "YGM平台BHPC资产显示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "区号", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "timeStamp", value = "时间搓", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "nonce", value = "随机数", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "signature", value = "签名规则", required = true, paramType = "form", dataType = "string"),
    })
    @SystemLog(description = "YGM平台查询BHPC资产显示")
    @PostMapping("/show_bhpc_balance")
    public Resp showBhpcBalance(String areaCode , String phone) throws Exception {
        if (ParamUtil.isBlack(areaCode)) {
            log.error("获取失败,区号为空!");
            return new Resp(Resp.PARAM_ERROR,"获取失败,区号为空!");
        }
        if (ParamUtil.isBlack(phone)) {
            log.error("获取失败,手机号为空!");
            return new Resp(Resp.PARAM_ERROR,"获取失败,手机号为空!");
        }
        User user = userRepository.findByPhoneAndAreaCode(phone,areaCode);
        if (user == null){
            log.error("获取失败,用户数据异常!");
            return new Resp(Resp.PARAM_ERROR,"获取失败,用户数据异常!");
        }
        BhpcWallet bhpcWallet = walletContent.bhpcWalletRepository.findByUserId(user.getUserId());
        if(bhpcWallet == null){
            bhpcWallet = myWallet.createBhpcWallet(user.getUserId());
            log.error("没有钱包数据,创建钱包");
        }
        return new Resp(bhpcWallet.getAvailBalance());
    }
}
